#include "prelistwid.h"
#include <QDebug>
#include "protreeitem.h"
#include "const.h"
#include <QPainter>
#include "prelistitem.h"
#include <QGuiApplication>

PreListWid::PreListWid(QWidget *parent) : QListWidget(parent), _global(0), _last_index(17)
{
    this->setViewMode(QListWidget::IconMode);       // 展示模式设置为只展示图标
    this->setIconSize(QSize(PREICON_SIZE, PREICON_SIZE));       // 设置图标大小
    this->setSpacing(5);            //  设置间隔为5

    connect(this, &PreListWid::itemPressed, this, &PreListWid::SlotItemPressed);
}

PreListWid::~PreListWid()
{

}

void PreListWid::AddListItem(const QString &path)
{
    QPixmap src_pixmap(path);
    src_pixmap = src_pixmap.scaled(PREICON_SIZE, PREICON_SIZE,  Qt::KeepAspectRatio);       // PREICON_SIZE 为 90
    // 目的图片
    QPixmap dist_pixmap(QSize(PREICON_SIZE, PREICON_SIZE));
    dist_pixmap.fill(QColor(220,220,220,50));
    QPainter painter(&dist_pixmap);
    // 原图大小
    auto src_width = src_pixmap.width();        // 原图的宽度
    auto src_height = src_pixmap.height();       // 原图的高度
    // // 目的图大小
    auto dist_width = dist_pixmap.width();
    auto dist_height = dist_pixmap.height();
    auto x = (dist_width - src_width) /2;          // 找到展示图片的起始位置
    auto y = (dist_height - src_height) /2;
    painter.drawPixmap(x, y, src_pixmap);           // 绘图
    _global++;

    PreListItem *pItem = new PreListItem(QIcon(dist_pixmap), path, _global, this);
    pItem->setSizeHint(QSize(PREITEM_SIZE, PREITEM_SIZE));
    this->addItem(pItem);
    _set_item[path] = pItem;
    if(_global == 1){   // 如果为第一张图片
        _pos_origin = this->pos();
    }

}

void PreListWid::SlotUpPreList(QTreeWidgetItem* tree_item)
{
    if(!tree_item){
        qDebug() << "tree_item is empty";
        return ;
    }

    auto *pro_item = dynamic_cast<ProTreeItem*>(tree_item);
    auto path = pro_item->GetPath();
    auto iter = _set_item.find(path);
    if(iter != _set_item.end()){
        qDebug() << "path " << " exists ";
        return ;
    }

    AddListItem(path);
}

void PreListWid::SlotUpSelect(QTreeWidgetItem *tree_item)
{
    if(!tree_item){
        return ;
    }

    auto *pro_item = dynamic_cast<ProTreeItem*>(tree_item);
    auto path = pro_item->GetPath();
    auto iter = _set_item.find(path);
    if(iter == _set_item.end()){        // 判断迭代器在item中是否存在
        return ;        // 不存在
    }

    auto *list_item = dynamic_cast<PreListItem*>(iter.value());
    auto index = list_item->GetIndex();
    if(index > 17){                                 // 大于17，表示需要翻页了
        auto pos_cur = this->pos();
        this->move(pos_cur.x() - (index - _last_index)*100,  pos_cur.y());      // 横坐标改变，纵坐标不变
    }else{
        this->move(_pos_origin);        // 小于17，无需移动
        _last_index = 17;
    }

    this->setCurrentItem(iter.value());
}

void PreListWid::SlotItemPressed(QListWidgetItem *item)
{
    if(QGuiApplication::mouseButtons() != Qt::LeftButton){
        return ;
    }

    auto *list_item = dynamic_cast<PreListItem*>(item);
    auto cur_index = list_item->GetIndex();
    auto path = list_item->GetPath();
    this->setCurrentItem(item);
    emit SigUpSelectShow(path);
}
